import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;

/**
 * @file InfoCanvas.java
 * @author xiao
 * @mail 158430154@qq.com
 * @data 2014年4月22日 下午11:16:32
 * @description: 信息绘制类
 */
public class InfoCanvas extends Canvas implements CommandListener {

	private DisplayCanvas midlet;
	private Display display;
	private MainScreen preScreen;
	private FrmSet frmset;
	private About about;
	private String strInfo;

	private int line;
	private int width;
	private int height;
	private int word_height;
	private int word_width;
	private int words_per_line;
	private int words_total_number;
	private int lines_per_screen;
	private int screen = 1;
	private int screens_total_number;
	private int vMove = 0;
	private String[] string_per_line;
	private int trans;

	Command cmdSet = new Command("显示设置", Command.ITEM, 1);
	Command cmdMark = new Command("书签管理", Command.ITEM, 1);
	Command addMark = new Command("加入书签", Command.ITEM, 1);
	Command cmdScreen = new Command("全屏", Command.EXIT, 1);
	Command cmdScreemFull = new Command("退出全屏", Command.EXIT, 1);
	Command cmdAbout = new Command("关于", Command.ITEM, 1);
	Command cmdExit = new Command("返回", Command.ITEM, 1);

	private int wordColorSelect = 0;
	private int BGColorSelect = 0;
	private int wordColor = 16777215;
	private int BGColor = 0;
	private int fontStyle = Font.STYLE_PLAIN;
	private int fontFace = Font.FACE_SYSTEM;
	private int fontSize = Font.SIZE_MEDIUM;
	private int fontStyleSelect = 0;
	private int fontFaceSelect = 0;
	private int fontSizeSelect = 0;

	public InfoCanvas( DisplayCanvas midlet, Display display, String string) {
		this.midlet = midlet;
		this.strInfo = string;
		this.display = display;

		this.addCommand(cmdSet);
		this.addCommand(cmdExit);
		this.addCommand(cmdMark);
		this.addCommand(addMark);
		this.addCommand(cmdScreen);
		this.addCommand(cmdAbout);
		setColor(wordColorSelect, BGColorSelect);
		setFont(fontFaceSelect, fontStyleSelect, fontSizeSelect);
		this.setCommandListener(this);
	}

	public void init() {
		width = getWidth() - 15;
		height = this.getHeight();
		// 获取字宽
		word_width = Font.getFont(fontStyle, fontStyle, fontSize).charWidth('爱');
		// 获取字高
		word_height = Font.getFont(fontStyle, fontStyle, fontSize).getHeight();
		// 获取文档字数
		words_total_number = strInfo.length();
		// 获取每行字数
		words_per_line = width / word_width;
		// 总行数
		line = words_total_number / words_per_line;
		// 获取每页行数
		lines_per_screen = height / word_height;
		// 总页数
		screens_total_number = line / lines_per_screen + 1;
		// 获取每行字符串
		string_per_line = new String[line + 1];
		for (int i = 0; i < line + 1; i++) {
			if (i == line) {
				string_per_line[i] = strInfo.substring((i * words_per_line), strInfo.length());
			} else {
				string_per_line[i] = strInfo.substring((i * words_per_line), ((i + 1) * words_per_line));
			}

		}

		trans = getHeight() / screens_total_number;

	}

	protected void paint(Graphics g) {
		// TODO Auto-generated method stub
		init();
		g.setColor(BGColor);
		g.fillRect(0, 0, width + 15, height);
		g.setColor(wordColor);
		g.setFont(Font.getFont(fontFace, fontStyle, fontSize));
		if (screen < screens_total_number) {
			for (int i = 0; i < lines_per_screen; i++) {
				g.drawString(string_per_line[i + vMove], 0, word_height * i, 20);
			}

		} else {

			for (int i = 0; i < line - vMove + 1; i++) {
				g.drawString(string_per_line[i + vMove], 0, word_height * i, 20);
			}
		}
		// 滑块
		g.setColor(255, 255, 255);
		g.fillRect(235, 0, 3, 320);
		g.setColor(128, 128, 128);
		g.fillRect(234, trans * screen, 5, 5);
	}

	/**
	 * @param word 所选字体颜色
	 * @param BG 所选背景颜色
	 * @author tan
	 * @mail 625844895@qq.com
	 */
	public void setColor(int word, int BG) {
		switch (word) {
		case 0:
			wordColor = 0; // 黑色
			wordColorSelect = 0;
			break;
		case 1:
			wordColor = 16777215;// 白色
			wordColorSelect = 1;
			break;
		case 2:
			wordColor = 16711680;// 红色
			wordColorSelect = 2;
			break;
		case 3:
			wordColor = 16776960;// 黄色
			wordColorSelect = 3;
			break;
		case 4:
			wordColor = 32768;// 绿色
			wordColorSelect = 4;
			break;
		case 5:
			wordColor = 255; // 蓝色
			wordColorSelect = 5;
			break;
		default:
			break;
		}

		switch (BG) {
		case 0:
			BGColor = 16777215;// 白色
			BGColorSelect = 0;
			break;
		case 1:
			BGColor = 0; // 黑色
			BGColorSelect = 1;
			break;
		case 2:
			BGColor = 16711680;// 红色
			BGColorSelect = 2;
			break;
		case 3:
			BGColor = 16776960;// 黄色
			BGColorSelect = 3;
			break;
		case 4:
			BGColor = 32768;// 绿色
			BGColorSelect = 4;
			break;
		case 5:
			BGColor = 255; // 蓝色
			BGColorSelect = 5;
			break;
		default:
			break;
		}
		repaint();
	}

	//
	public void setScreen(int screen) {
		this.vMove = (screen - 1) * lines_per_screen;
		repaint();
	}

	public String getMarkString(int screen) {
		return string_per_line[(screen - 1) * lines_per_screen].substring(0, 7);
	}

	//
	protected void keyPressed(int keyCode) {
		int action = this.getGameAction(keyCode);

		switch (action) {
		case Canvas.FIRE:

			break;
		case Canvas.DOWN:
			if (screen == screens_total_number) {

			} else {
				vMove = vMove + lines_per_screen;
				screen++;
			}
			break;
		case Canvas.UP:
			if (vMove == 0) {
				vMove = 0;
			} else {
				vMove = vMove - lines_per_screen;
				screen--;
			}
			break;
		case 0: {
			if (keyCode == -6) {// left key

			} else if (keyCode == -7) {// right key

			}
			break;
		}
		default:
			break;
		}
		repaint();
		serviceRepaints();
	}

	/**
	 * @author zemo
	 * @mail zemochen@gmail.com
	 */
	public void commandAction(Command c, Displayable d) {
		// TODO Auto-generated method stub
		if (c.getLabel().equalsIgnoreCase("全屏")) {
			removeCommand(cmdScreen);
			addCommand(cmdScreemFull);
			setFullScreenMode(true);
			init();

		} else if (c.getLabel().equalsIgnoreCase("退出全屏")) {
			removeCommand(cmdScreemFull);
			addCommand(cmdScreen);
			setFullScreenMode(false);
			init();

		} else if (c.getLabel().equalsIgnoreCase("显示设置")) {
			frmset = new FrmSet(this, display);
			display.setCurrent(frmset);
		} else if (c.getLabel().equalsIgnoreCase("书签管理")) {
			display.setCurrent(new MarkList(this, display));
		} else if (c.getLabel().equalsIgnoreCase("加入书签")) {
			try {
				RecordStore rmsmark = RecordStore.openRecordStore("rms", true);
				int ID = rmsmark.getNumRecords() + 1;
				String rms = String.valueOf(screen) + String.valueOf(wordColorSelect) + String.valueOf(BGColorSelect)
						+ "," + String.valueOf(ID);

				byte[] data = rms.getBytes();
				rmsmark.addRecord(data, 0, data.length);

				rmsmark.closeRecordStore();
			} catch (RecordStoreException rse) {
				System.err.println(rse.toString());
			}

			Alert alert = new Alert("成功！", "添加书签成功，请在书签管理中查看", null, AlertType.INFO);
			alert.setTimeout(2000);
			display.setCurrent(alert);
		} else if (c.getLabel().equalsIgnoreCase("返回")) {
			display.setCurrent(preScreen);
		} else if (c.getLabel().equals("关于")) {
			about = new About("小说阅读器--关于我们");
			display.setCurrent(about);
		}
	}

	/**
	 * @param word 所选字体颜色
	 * @param BG 所选背景颜色
	 * @author tan
	 * @mail 625844895@qq.com
	 */
	public void setFont(int face, int style, int size) {
		switch (face) {
		case 0:
			fontFace = Font.FACE_MONOSPACE;
			fontFaceSelect = 0;
			break;
		case 1:
			fontFace = Font.FACE_PROPORTIONAL;
			fontFaceSelect = 1;
			break;
		case 2:
			fontFace = Font.FACE_SYSTEM;
			fontFaceSelect = 2;
			break;
		default:
			break;
		}

		switch (style) {
		case 0:
			fontStyle = Font.STYLE_PLAIN;
			fontStyleSelect = 0;
			break;
		case 1:
			fontStyle = Font.STYLE_BOLD;
			fontStyleSelect = 1;
			break;
		case 2:
			fontStyle = Font.STYLE_ITALIC;
			fontStyleSelect = 2;
			break;
		default:
			break;
		}

		switch (size) {
		case 0:
			fontSize = Font.SIZE_LARGE;
			fontSizeSelect = 0;
			break;
		case 1:
			fontSize = Font.SIZE_MEDIUM;
			fontSizeSelect = 1;
			break;
		case 2:
			fontSize = Font.SIZE_SMALL;
			fontSizeSelect = 2;
			break;
		default:
			break;
		}

	}

}
